package com.base.cn.platform.os.controller.manage.user;

import com.base.cn.platform.os.common.controller.BaseController;
import com.base.cn.platform.os.common.mybatis.Pagination;
import com.base.cn.platform.os.common.utils.DateUtils;
import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.StringUtils;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.manage.user.SysUser;
import com.base.cn.platform.os.service.manage.user.SysUserBiz;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
/**
 * 管理员用户Controller
 *
 * @author s.li
 * @create 2018-02-06 14:33
 */
@RestController
@RequestMapping("/manage")
public class SysUserController extends BaseController {

    @Autowired
    private SysUserBiz sysUserBiz;

    /**
     * 查询用户数量
     * @return 用户数量
     */
    @RequestMapping("/sysUserCount")
    public BigDecimal count(){
        return sysUserBiz.count(" id!=1 and status!=3");
    }

    /**
     * 查询管理员用户列表
     * @param currentPage 分页条件
     * @param sysUser 查询条件
     * @param roleData 是否查询角色数据
     * @param groupData 是否查询部门数据
     * @param createUser 是否查询创建人的数据
     * @return  PageInfo<SysUser>
     */
    @RequestMapping("/querySysUserList")
    public PageInfo<SysUser> querySysUserList(@RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage,
                                              @RequestBody SysUser sysUser,
                                              @RequestParam("roleData") boolean roleData,
                                              @RequestParam("groupData") boolean groupData,
                                              @RequestParam("createUser") boolean createUser){
        Pagination page = new Pagination();
        page.setCurrentPage(currentPage);
        PageInfo<SysUser> sysUserPageInfo = sysUserBiz.querySysUserList(page,sysUser,roleData,groupData,createUser);
        return sysUserPageInfo;
    }

    /**
     * 通过用户名查询用户数据对象
     * @param username 用户名
     * @return 存在则返回用户对象，不存在则返回null
     */
    @RequestMapping("/findSysUserByUsername")
    public SysUser findSysUserByUsername(String username){
        return sysUserBiz.findSysUserByUsername(username);
    }

    /**
     * 通过用户ID，查询用户对象
     * @param userId 用户ID
     * @param roleData 是否设置用户的角色数据
     * @param groupData  是否设置用户的部门数据
     * @param createUser 是否设置用户的创建人数据
     * @return SysUser
     */
    @RequestMapping("/findSysUserById")
    public SysUser findSysUserById(@RequestParam("userId") BigDecimal userId,
                                   @RequestParam("roleData") boolean roleData,
                                   @RequestParam("groupData") boolean groupData,
                                   @RequestParam("createUser") boolean createUser){
        return sysUserBiz.findSysUserById(userId,roleData,groupData,createUser);
    }

    /**
     * 通过用户ID串，获取用户Map
     * @param userIds 用户ID串
     * @return Map<BigDecimal,SysUser>
     */
    @RequestMapping("/findSysUserMapByUserIds")
    public Map<BigDecimal,SysUser> findSysUserMapByUserIds(@RequestParam("userId") String userIds,
                                                           @RequestParam("roleData") boolean roleData,
                                                           @RequestParam("groupData") boolean groupData,
                                                           @RequestParam("createUser") boolean createUser){
        return sysUserBiz.findSysUserMapByUserIds(userIds,roleData,groupData,createUser);
    }

    /**
     * 修改用户状态
     * @param userIds 用户ID串
     * @param status 状态，1正常，2禁用
     */
    @RequestMapping("/updateSysUserStatus")
    public Map<String,Object> updateSysUserStatus(@RequestParam("userIds") String userIds,@RequestParam("status") int status){
        sysUserBiz.updateSysUserStatus(userIds,status);
        return ResultUtil.SUCCESS("用户状态修改成功");
    }

    /**
     * 删除用户
     * @param userIds 用户ID串
     * @return Map<String,Object>
     */
    @RequestMapping("/deleteSysUser")
    public Map<String,Object> deleteSysUser(@RequestParam("userIds") String userIds){
        sysUserBiz.deleteSysUser(userIds);
        return ResultUtil.SUCCESS("用户删除成功");
    }

    /**
     * 延期体验用户过期时间
     * @param userIds 用户ID串
     * @return Map<String,Object>
     */
    @RequestMapping("/delayedUser")
    public Map<String,Object> delayedUser(@RequestParam("userIds") String userIds){
        Date endTime = sysUserBiz.delayedUser(userIds);
        if(endTime!=null){
            return ResultUtil.SUCCESS("体验用户延期成功", DateUtils.format(endTime,"yyyy-MM-dd HH:mm"));
        }else{
            return ResultUtil.SUCCESS("体验用户延期成功");
        }
    }

    /**
     * 修改用户密码
     * @param userId 用户ID
     * @param newPassword 用户新密码
     * @return Map<String,Object>
     */
    @RequestMapping("/updateUserPwd")
    public Map<String,Object> updateUserPwd(@RequestParam("userId") BigDecimal userId,@RequestParam("newPassword") String newPassword){
        if(ObjectUtils.isEmpty(userId) || userId.compareTo(new BigDecimal(0)) <=0 ){
            return ResultUtil.ERROR("用户不存在，密码修改失败");
        }
        if(!StringUtils.isLetterOrNum(newPassword)
                || newPassword.trim().length()<6
                || newPassword.trim().length()>20){
            return ResultUtil.ERROR("密码必须是数字或英文，且大6位小于20位");
        }
        sysUserBiz.updateSysUserPwd(userId,newPassword);
        return ResultUtil.SUCCESS("用户密码修改成功");
    }

    /**
     * 根据账号姓名查询id串
     * @param userName
     * @return
     */
    @RequestMapping("/getUserNameIds")
    public String getUserNameIds(@RequestParam("userName") String userName){

       return sysUserBiz.getUserNameIds(userName);
    }

    /**
     * 修改用户引导状态
     * @param userId 用户id
     * @param guideStatus 引导状态 1-未引导，2-已引导
     */
    @RequestMapping("/updateSysUserGuideStatus")
    public void updateSysUserGuideStatus(@RequestParam("userId") BigDecimal userId,
                                         @RequestParam("guideStatus") Integer guideStatus){
        SysUser sysUser = new SysUser();
        sysUser.setId(userId);
        sysUser.setGuideStatus(guideStatus);
        sysUserBiz.updateById(sysUser,null);
    }
}
